//
//  SSSSetUpController.m
//  SSSAllPeopleBenefit
//
//  Created by Darker on 2017/2/24.
//  Copyright © 2017年 Darker. All rights reserved.
//

#import "SSSSetUpController.h"
#import "SSSSetUpCommonCell.h"
#import "accountDetailController.h"
#import "RecommendedFeedbackController.h"
#import "helpController.h"
#import "SSSLoginViewController.h"
#import "AboutOurController.h"

@interface SSSSetUpController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    NSArray *_contentArray;//数据源数组
    UITableView * _bgTableView;//tableView
    
}

@end

static NSString * const setUpCommonID = @"setUpCommonCell";

@implementation SSSSetUpController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.title = @"设置";
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //1.初始化数据源
    [self initDataSource];
    
    //2.初始化tableView
    [self initBgTableView];
}
#pragma mark - 初始化数组
- (void)initDataSource{
    
    _contentArray = @[@"账户详情",@"清除缓存",@"当前版本"];
}
#pragma mark - 初始化tableView
- (void)initBgTableView{
    
    _bgTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,84, FSCREEN_Width, HeigthIos6CompeteWithCurrent(50)*_contentArray.count) style:UITableViewStylePlain];
    _bgTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _bgTableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    _bgTableView.backgroundColor = [UIColor colorWithHex:@"FFFFFF"];
    //_bgTableView.backgroundColor = [UIColor whiteColor];
    _bgTableView.delegate = self;
    _bgTableView.dataSource = self;
    _bgTableView.rowHeight = HeigthIos6CompeteWithCurrent(50);
    _bgTableView.scrollEnabled = NO;
    [self.view addSubview:_bgTableView];
    //注册cell
    [_bgTableView registerNib:[UINib nibWithNibName:@"SSSSetUpCommonCell" bundle:nil] forCellReuseIdentifier:setUpCommonID];
    
    
    //退出登录按钮
    UIButton *quitBtn = [[UIButton alloc]init];
    [quitBtn setTitle:@"退出账号" forState:UIControlStateNormal];
    [self.view addSubview:quitBtn];
    [quitBtn setTitleColor:[UIColor colorWithHex:@"6D6D6D"] forState:UIControlStateNormal];
    [quitBtn addTarget:self action:@selector(quitLogin) forControlEvents:UIControlEventTouchDown];
    
    [quitBtn setBackgroundColor:[UIColor colorWithHex:@"FFFFFF"]];
    quitBtn.sd_layout
    .topSpaceToView(_bgTableView,HeigthIos6CompeteWithCurrent(10))
    .leftEqualToView(_bgTableView)
    .rightEqualToView(_bgTableView)
    .heightIs(HeigthIos6CompeteWithCurrent(51));
}
#pragma amrk - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _contentArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SSSSetUpCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:setUpCommonID];
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.nameLabel.text = _contentArray[indexPath.row];
        
        cell.nameLabel.text = _contentArray[indexPath.row];
    
         if (indexPath.row == 0) {
             
            cell.subTitle.hidden = YES;
             
        }else if (indexPath.row == 1){
            
            SSSClearCacheManager *clearCacheManager = [SSSClearCacheManager zd_sharedCache];
            CGFloat cacheSize = [clearCacheManager zd_loadCacheSize];

            cell.subTitle.text = [NSString stringWithFormat:@"%.2fM",cacheSize];
            
        }else if (indexPath.row == 2){
            
            //获取版本号信息
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            cell.subTitle.text = app_Version;
            cell.lineView.hidden = YES;
            
        }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        accountDetailController *accountVc = [[accountDetailController alloc]init];
        [self.navigationController pushViewController:accountVc animated:YES];
        
    }else if(indexPath.row == 1){
        SSSClearCacheManager *clearCacheManager = [SSSClearCacheManager zd_sharedCache];
        CGFloat cacheSize = [clearCacheManager zd_loadCacheSize];
        
        NSString *clearMessage = [NSString stringWithFormat:@"缓存大小为%.2fM确定要清理吗?", cacheSize];
        
        QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:^(QMUIAlertAction *action) {
            return ;
        }];
        QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"确定" style:QMUIAlertActionStyleDestructive handler:^(QMUIAlertAction *action) {
            
            
            [clearCacheManager zd_myClearCacheAction];
            
            [_bgTableView cellForRowAtIndexPath:indexPath];
            
            SSSSetUpCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:setUpCommonID];
            
            if (indexPath.row == 1) {
                cell.subTitle.text = @"0";
                [_bgTableView reloadData];
            }
            
        }];
        QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"温馨提示" message: clearMessage preferredStyle:QMUIAlertControllerStyleAlert];
        [alertController addAction:action1];
        [alertController addAction:action2];
        [alertController showWithAnimated:YES];
    }
    
    else if (indexPath.row == 2){
        
    }else if (indexPath.row == 3){
     
        
        
    }else if (indexPath.row == _contentArray.count - 1){
        
//        helpController *helpVc = [[helpController alloc]init];
//        [self.navigationController pushViewController:helpVc animated:YES];
    }
    
}
- (void)quitLogin{
    
    SSSLoginViewController *loginVc = [[SSSLoginViewController alloc]init];
    // [self.navigationController pushViewController:loginVc animated:YES];
    self.view.window.rootViewController = loginVc;
    
}
@end

